﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using wpf4gp.ViewModels;
using wpf4gp.Views;

namespace wpf4gp
{

    /// <summary>
    /// Extension methods
    /// </summary>
    public static class Extensions
    {

        /// <summary>
        /// Show window with Exception details
        /// </summary>
        /// <param name="ex">Exception to show</param>
        public static void ShowDetails(this Exception ex)
        {
            // Visualizzo il messaggio di errore
            Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() =>
            {
                ExceptionDetailsWindow view = new ExceptionDetailsWindow();
                ExceptionDetailsViewModel vm = new ExceptionDetailsViewModel(ex);
                view.DataContext = vm;
                if (Application.Current.MainWindow != null)
                {
                    view.Owner = Application.Current.MainWindow;
                }
                view.ShowDialog();
            }));
        }

        /// <summary>
        /// Show window message box for the exception
        /// </summary>
        /// <param name="ex"><see cref="System.Exception"/> to show</param>
        /// <param name="message">The message to show (if not passed, will see the exception message)</param>
        /// <param name="title">Title of the window</param>
        /// <param name="canViewDetails">False if the user can't see the detail of the Exception</param>
        public static void ShowMessageBox(this Exception ex, string message = null, string title = null, bool canViewDetails = true)
        {
            // Visualizzo il messaggio di errore
            Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() =>
            {
                ExceptionMessageBox view = new ExceptionMessageBox();
                ExceptionMessageBoxViewModel vm = new ExceptionMessageBoxViewModel(ex, message, title, canViewDetails);
                view.DataContext = vm;
                if (Application.Current.MainWindow != null)
                {
                    view.Owner = Application.Current.MainWindow;
                }
                try
                {
                    view.ShowDialog();
                }
                catch { }
            }));
        }

    }
}
